package com.lance.answer.question_export;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.lance.answer.dto.Item;
import com.lance.answer.dto.QuestionAnswer;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.util.MD5Utils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class AnswerFix {

    /**
     * 	"questionCode":"7174AD2EF06A082270F135FA5298BF9D",
     * 		"questionTitle":"个体通过社会化得以（ ），获得发展的基点。",
     * @param args
     */
    public static void main(String[] args) throws IOException {
        fix("调剂学");
       // fix("Dreamweaver网页设计");
        //System.out.println(MD5Utils.encode("药物化学"+ "上皮组织的特点，不包括（　）。") );
    }

    public static void fix(String subjectName) throws IOException {
        subjectName = subjectName.trim();
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(subjectName + "/" + subjectName + "-answer.json");

        String answerStr1 = IOUtils.toString(inputStream,"utf-8");
        answerStr1 = answerStr1.replace("\\C","");

        List<QuestionAnswer> list = JSONObject.parseArray(answerStr1,QuestionAnswer.class);
        System.out.println(list.size());
        int i = 0;
        for (QuestionAnswer answerObject:list) {
            String questionTitle = Util.dealContent(answerObject.getQuestionTitle());
            String questionCodeSrc = answerObject.getQuestionCode();
            String questionCode = MD5Utils.encode(subjectName + questionTitle);
            if (!Objects.equals(questionCodeSrc,questionCode)) {
                System.out.println(questionCodeSrc + " ---> " + questionCode);
            }
            answerObject.setQuestionCode(questionCode);
            if (StringUtils.isEmpty(answerObject.getAnswerType())) {
                answerObject.setAnswerType("click");
            }
            List<String> realAnswers = new ArrayList<>();
            // FIXME 10-21 add
            if(null == answerObject.getAnswers()){
                continue;
            }

            if(answerObject.getQuestionType()==2){
                List<String> originAnswers = answerObject.getAnswers();
                if(originAnswers.size()==1){
                    String answer = originAnswers.get(0);
                    List<String> rAnswers = Lists.newArrayList();
                    rAnswers.addAll(Arrays.asList(answer.split(" ")));
                    answerObject.setAnswers(rAnswers);
                }

            }

            // FIXME 10-21 add
            for (String item:answerObject.getAnswers()) {
                // fixme 10-28
                if(null != answerObject.getItems()){
                    for(Item rItem:answerObject.getItems()){
                        //if(item.equals(rItem.getPrefix())){
                        if(Strings.isNullOrEmpty(rItem.getPrefix())){
                            continue;
                        }
                        if(item.toUpperCase(Locale.ROOT).equals(rItem.getPrefix().substring(0,1).toUpperCase(Locale.ROOT))){
                            item = rItem.getContent();
                        }
                    }
                }else {
                    System.err.println(answerObject.getQuestionTypeDesc());
                }
                if (item.endsWith(" ")) {
                    i++;
                }
               realAnswers.add(Util.dealContent(item));
            }
            answerObject.setAnswers(realAnswers);
        }


        System.out.println(i);
        String path = "src/main/resources/" + subjectName + "/" + subjectName + "-answer1.json";
        FileUtils.write(new File(path), JSONObject.toJSONString(list, SerializerFeature.PrettyFormat),"utf-8");
    }
}
